<template>
	<view>
		<!-- <button @click="select">选择图片</button> -->
		<image mode="widthFix" :src="path" />
		<ksp-cropper mode="fixed" :width="300" :height="300" :maxWidth="1024" :maxHeight="1024" :url="url"
			@cancel="oncancel" @ok="onok"></ksp-cropper>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: "",
				path: ""
			}
		},
		onLoad(options) {
			this.url = decodeURIComponent(options.path)
		},
		methods: {
			select() {
				uni.chooseImage({
					count: 1,
					sourceType: ['camera'],
					success: (rst) => {
						// 设置url的值，显示控件
						this.url = rst.tempFilePaths[0];
					}
				});
			},
			onok(ev) {
				this.url = "";
				this.path = ev.path;
				uni.uploadFile({
					url: this.$baseUrl + '/Base_Manage/Upload/Filleimag',
					filePath: this.path,
					name: 'file'
				}).then(result => {
					console.log(result);
					let userInfo = uni.getStorageSync('userInfo') || {}
					userInfo.avatarUrl = JSON.parse(result[1].data).Data
					uni.setStorageSync('userInfo', userInfo)
					this.uploadAvatar(userInfo.UserPhone, JSON.parse(result[1].data).Data)
				})
			},
			oncancel() {
				// url设置为空，隐藏控件
				this.url = "";
				uni.navigateBack()
			},
			uploadAvatar(phone, avatar) {
				uni.request({
					url: this.$veriUrl + '/auth/VeriRecord/avatar',
					method: 'POST',
					data: {
						phone,
						avatar
					}
				}).then(result => {
					let [err, res] = result;
					if (res.data.code == 200) {
						uni.navigateBack()
					} else {
						uni.showToast({
							icon: 'none',
							title: '上传失败'
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)()
					}
				})
			}
		}
	}
</script>